package com.example.bootdemo.config;

import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.util.Locale;

// 既保留了springboot对web开发的默认配置，又可以自定义的配置
//@Configuration //让下面的实现类声明为配置类,能被Spring Boot扫描到并生成实例存到容器中
public class MyMVCconfig implements WebMvcConfigurer {
    //添加视图管理
//    @Override
//    public void addViewControllers(ViewControllerRegistry registry) {
//        //请求toLoginPage映射路径或者Login.html页面都会自动映射到Login.html页面
//        registry.addViewController("toLoginPage").setViewName("login");
//        registry.addViewController("login.html").setViewName("login");
//
//    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
//        localeChangeInterceptor.setParamName("lang");
//        registry.addInterceptor(localeChangeInterceptor);

        //将我们自定义的拦截器加入配置
        InterceptorRegistration interceptor = registry.addInterceptor(new MyInterceptor());
        //设置拦截的url
        interceptor.addPathPatterns("/home/**");
    }

//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        registry.addResourceHandler("/**").addResourceLocations("/");
//    }



    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new StringToDateConverter());
    }


}
